Imports MIMS.WRS.Business
Imports MIMS.WRS.Entities
Imports System.IO
Imports System.Collections
Imports MyFramework
Imports System.Xml

Public Class CustomerOrderDetails
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents dlShoppingList As System.Web.UI.WebControls.DataList
    Protected WithEvents btnPrint As System.Web.UI.WebControls.Button
    Protected WithEvents lblCompanyName As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler dlShoppingList.ItemDataBound, AddressOf dlShoppingList_ItemDataBound

        If (Not IsPostBack) Then
            Dim result As New ShoppingListCollection
            result = ShoppingListService.GetShoppingListByShoppingOrderId(ShoppingOrderId)
            If (Not result Is Nothing AndAlso result.Count > 0) Then
                TerritoryID = result(0).TerritoryID
            End If
            dlShoppingList.DataSource = result
            dlShoppingList.DataBind()

            Try
                lblCompanyName.Text = CompanyName
            Catch ex As Exception
            End Try
        End If
    End Sub

    Private Sub dlShoppingList_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim ltrItemIndex As Label = CType(e.Item.FindControl("ltrItemIndex"), Label)
            ltrItemIndex.Text = (e.Item.ItemIndex + 1).ToString()

        End If
    End Sub

    Property ShoppingOrderId() As Integer
        Get
            If (viewstate("ShoppingOrderId") Is Nothing) Then
                If Not (Request("Id") Is Nothing) Then
                    viewstate("ShoppingOrderId") = Request("Id")
                Else
                    viewstate("ShoppingOrderId") = 0
                End If
            End If
            Return Convert.ToInt32(viewstate("ShoppingOrderId"))
        End Get
        Set(ByVal Value As Integer)
            viewstate("ShoppingOrderId") = Value
        End Set
    End Property

    Property TerritoryID() As String
        Get
            Return Convert.toString(IIf(viewstate("TerritoryID") Is Nothing, "", viewstate("TerritoryID")))
        End Get
        Set(ByVal Value As String)
            viewstate("TerritoryID") = Value
        End Set
    End Property

    ReadOnly Property CompanyName() As String
        Get
            Try
                Dim name As String = Request("secondValue")
                name = name.Replace("~", "'")
                Return name
            Catch ex As Exception
            End Try
            Return ""
        End Get
    End Property

    Protected Function GetFOBorHDHeader() As String
        Return Convert.toString(IIf(TerritoryID > "", "Freight Paid (Home Delivered)", "FOB"))
    End Function

    Protected Function GetRowClass(ByVal container As Object) As String
        Dim item As DataListItem = CType(container, DataListItem)

        If item.ItemIndex Mod 2 = 0 Then
            Return "ItemRow"
        Else
            Return "AlternateItemRow"
        End If

    End Function

    Protected Function GetPriority(ByVal data As Object) As String
        Dim result As String = ""
        If Not data Is DBNull.Value Then
            Select Case (Convert.ToInt32(data))
                Case 1
                    result = "Increase"
                Case 2
                    result = "Exact"
                Case 3
                    result = "Reduce"
            End Select
        End If
        Return result
    End Function

    Protected Function GetDecimalText(ByVal data As Object) As String
        Dim result As String = ""
        Dim value As Double = 0
        If Not data Is DBNull.Value Then
            value = Convert.ToDouble(data)
        End If
        If (value <> 0) Then
            result = value.ToString(Config.DecimalFormat2)
        End If
        Return result
    End Function

    Protected Sub btnPrint_OnClick(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnPrint.Click
        Try
            Dim source As IDataReader = ShoppingListService.GetShoppingListByShoppingOrderIdReport(ShoppingOrderId)
            Dim boXml As String = RenderService.GenerateOrdersBOXML(source, GetFOBorHDHeader(), CompanyName)
            Dim xslFile As String = Server.MapPath("foxsl/foCustomerOrderDetailReport.xsl")
            Dim foFile As String = Server.MapPath("foTemp/" + String.Format("{0}{1}{2}{3}{4}{5}{6}.fo", DateTime.Now.Year, _
                    DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, _
                    DateTime.Now.Second, DateTime.Now.Millisecond))
            Dim pdfFile As String = String.Format("CustomerOrderDetailReport_{0}{1}{2}{3}{4}{5}{6}.pdf", DateTime.Now.Year, _
                    DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, _
                    DateTime.Now.Second, DateTime.Now.Millisecond)
            Try
                If RenderService.GenerateFO(boXml, xslFile, foFile) Then
                    Dim buffer() As Byte = RenderService.RenderFOtoPDF(foFile)
                    File.Delete(foFile)

                    Response.Clear()
                    Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", HttpUtility.UrlEncode(pdfFile)))
                    Response.AddHeader("Content-Type", "application/pdf")
                    Response.BinaryWrite(buffer)
                    Response.Flush()
                    Response.End()

                End If
            Catch ex As Exception
                Logger.Debug("Failed to render AvailReport PDF", ex)
            End Try
        Catch ex As Exception
        End Try
    End Sub

End Class
